#include "stdafx.h"
#include "FunctionHolder.h"


Library::FunctionHolder::FunctionHolder( const std::vector<std::pair<double, double> >& points, const int numberOfUnfixedPoints )
: numberOfUnfixedPoints_(numberOfUnfixedPoints)
{
    points_.resize(points.size());
    std::copy(points.begin(), points.end(), points_.begin());
    PushRandomPoints();

    Utils::CalculateDistances(points_, matrix_);
}

void Library::FunctionHolder::PushRandomPoints()
{
    for (int i = 0; i < numberOfUnfixedPoints_; ++i )
    {
        std::pair<double, double> temp((rand() % 201) - 100, (rand() % 201) - 100);
        points_.push_back(temp);
    }
}

void Library::FunctionHolder::ResetMutablePoints()
{
	for (int i = 0; i < numberOfUnfixedPoints_; ++i)
	{
		std::pair<double, double> temp((rand() % 201) - 100, (rand() % 201) - 100);
		points_[points_.size() - i - 1] = temp;
	}
}

void Library::FunctionHolder::Calculate()
{
    edges_.clear();
    Utils::PrimeAlgorithm(matrix_, edges_);
}

double Library::FunctionHolder::GetResult() const
{
    return Utils::CalculateLength(matrix_, edges_);
}

const std::vector<std::pair<double, double> >& Library::FunctionHolder::GetPoints() const
{
    return points_;
}

const std::vector<std::pair<int, int> >& Library::FunctionHolder::GetEdges() const
{
    return edges_;
}

const std::vector<std::pair<double, double> > Library::FunctionHolder::GetMutablePoints() const
{
    return std::vector<std::pair<double, double> >(points_.end() - numberOfUnfixedPoints_, points_.end());
}

void Library::FunctionHolder::SetMutablePoints( const std::vector<std::pair<double, double> >& points )
{
    points_.resize(points_.size() - points.size());
    points_.insert(points_.end(), points.begin(), points.end());

    for (size_t i = points_.size() - points.size(); i < points_.size(); ++i)
    {
        Utils::ChangeDistanceForOnePoint(points_, matrix_, i);
    }
}

void Library::FunctionHolder::SetEdges(std::vector<std::pair<int, int> >& edges)
{
	edges_ = edges;
}